const app = getApp()
let ServiceUrl = app.globalData.ServiceUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
      user_id: '',
      roleState: '',
      take_order_num: 0,
      auditing_num: 0,
      notice_um: 0,
      notice_nums: 0,
      pay_notice_num: 0
  },
  //用户下单新订单提醒
  noticeWarn:function(){
    let that = this;
    wx.requestSubscribeMessage({
        tmplIds: ['gCOf9OHaXUxkQ5WwFu2yz38QTLh3SEZYS5PDJd6XmlA'],
        success:(res)=> {
          //成功回调,向后台返回数据
          console.log(res)
          if(res["gCOf9OHaXUxkQ5WwFu2yz38QTLh3SEZYS5PDJd6XmlA"] == "accept"){
            //订阅成功,向后台返回数据
            wx.request({
              url: ServiceUrl + 'noticeWarn',
              data: {
                user_id: that.data.user_id
              },
              success: function(res){
                console.log(res)
                if(res.data.state == 200){
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                  })
                  that.setData({notice_num: that.data.notice_num + 1})
                }
              }
            })
          }else{
            console.log('用户拒绝订阅了');
          }
        },
      fail:(res)=>{
        console.log(res)
      }
    })
  },
  //用户下单新订单提醒
  noticeWarns:function(){
    let that = this;
    wx.requestSubscribeMessage({
        tmplIds: ['gCOf9OHaXUxkQ5WwFu2yz38QTLh3SEZYS5PDJd6XmlA'],
        success:(res)=> {
          //成功回调,向后台返回数据
          console.log(res)
          if(res["gCOf9OHaXUxkQ5WwFu2yz38QTLh3SEZYS5PDJd6XmlA"] == "accept"){
            //订阅成功,向后台返回数据
            wx.request({
              url: ServiceUrl + 'noticeWarns',
              data: {
                user_id: that.data.user_id
              },
              success: function(res){
                console.log(res)
                if(res.data.state == 200){
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                  })
                  that.setData({notice_nums: that.data.notice_nums + 1})
                }
              }
            })
          }else{
            console.log('用户拒绝订阅了');
          }
        },
      fail:(res)=>{
        console.log(res)
      }
    })
  },
  //派送订单订阅消息（陪诊接收消息）
  takeOrderWarn:function(){
    let that = this;
    wx.requestSubscribeMessage({
        tmplIds: ['wZGFsAC_DzBIY45gyqkJ-Fn2HDRE11C6uIIMT_QkzSc'],
        success:(res)=> {
          //成功回调,向后台返回数据
          console.log(res)
          if(res["wZGFsAC_DzBIY45gyqkJ-Fn2HDRE11C6uIIMT_QkzSc"] == "accept"){
            //订阅成功,向后台返回数据
            wx.request({
              url: ServiceUrl + 'takeOrderWarn',
              data: {
                user_id: that.data.user_id
              },
              success: function(res){
                console.log(res)
                if(res.data.state == 200){
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                  })
                  that.setData({take_order_num: that.data.take_order_num + 1})
                }
              }
            })
          }else{
            console.log('用户拒绝订阅了');
          }
        },
      fail:(res)=>{
        console.log(res)
      }
    })
  },
  //订阅审核次数
  auditingWarn:function(){
    let that = this;
    wx.requestSubscribeMessage({
        tmplIds: ['uRpi3UrTOxeRziuH0YYKLG05P_D8NMzVVvRBN2ijlDU','jv8sWF8pJYRAtGxW68lvvvSYNL6gqNXSQCCXyfpC4FY'],
        success:(res)=> {
          //成功回调,向后台返回数据
          console.log(res)
          if(res["uRpi3UrTOxeRziuH0YYKLG05P_D8NMzVVvRBN2ijlDU"] == "accept"){
            //订阅成功,向后台返回数据
            wx.request({
              url: ServiceUrl + 'auditingWarn',
              data: {
                user_id: that.data.user_id
              },
              success: function(res){
                console.log(res)
                if(res.data.state == 200){
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                  })
                  that.setData({auditing_num: that.data.auditing_num + 1})
                }
              }
            })
          }else{
            console.log('用户拒绝订阅了');
          }
        },
      fail:(res)=>{
        console.log(res)
      }
    })
  },
  //老汤接收支付成功的消息
  payNoticeNum:function(){
    let that = this;
    wx.requestSubscribeMessage({
        tmplIds: ['cVzp2qk9XfBdl-QuY9m-zqAZ8SBgLp91IGL1fPs6bhM'],
        success:(res)=> {
          //成功回调,向后台返回数据
          console.log(res)
          if(res["cVzp2qk9XfBdl-QuY9m-zqAZ8SBgLp91IGL1fPs6bhM"] == "accept"){
            //订阅成功,向后台返回数据
            wx.request({
              url: ServiceUrl + 'payNoticeNum',
              data: {
                user_id: that.data.user_id
              },
              success: function(res){
                console.log(res)
                if(res.data.state == 200){
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                  })
                  that.setData({pay_notice_num: that.data.pay_notice_num + 1})
                }
              }
            })
          }else{
            console.log('用户拒绝订阅了');
          }
        },
      fail:(res)=>{
        console.log(res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options)
      let that = this;
      that.setData({
        user_id: options.user_id,
        role_state: options.role_state
      })
      that.getSubInfo();
  },
  //发送请求获取数据
  getSubInfo(){
    let that = this
    wx.request({
      url: ServiceUrl + 'getSubscribeNum',
      data: {user_id: that.data.user_id},
      success: function(res){
        console.log(res);
        that.setData({
          take_order_num: res.data.data.take_order_num,
          auditing_num: res.data.data.auditing_num,
          notice_num: res.data.data.notice_num,
          pay_notice_num: res.data.data.pay_notice_num,
          notice_nums: res.data.data.notice_nums
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this
    wx.stopPullDownRefresh({
      success: (res) => {
        that.getSubInfo();
      },
    },3000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})